import {
  IMG_PATH
} from '../../../../config/index.js'
import Toast from '../../../../@vant/weapp/toast/toast';
import { ApiRootUrl } from '../../../../config/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gangweiId:'',
    orgCode:'',
    iseditText:'未编辑',
    isuploadText:'未上传',
    isjipin:false,
    appilicationShow:false,
    IMG_PATH:IMG_PATH,
    positionName:'',
    daiyuName:'',
    xinzi:'',
    tagList:[],
    postionDesc:'',
    managerPhone:'',
    oldPath:''
  },
  phoneClick(){
    let self = this;
    wx.makePhoneCall({
      phoneNumber: self.data.managerPhone,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  appilicationClose(){
    this.setData({
      appilicationShow:false
    })
  },
  editClick(){
    wx.navigateTo({
      url: '../editresume/editresume',
    })
  },
  queryClick(){
    console.log('dianjichakan ')
    let self = this;
    wx.downloadFile({
      url: self.data.oldPath,
      success: function (res) {
        console.log('res------',res)
        var filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
          }
        })
      }
    })
  },
  uploadClick(){
    let self = this;
    wx.chooseMessageFile({
      count: 1,
      type:'file',
      success(res){
        console.log('上传文件',res);
        if(res.tempFiles[0].size > 1024*1024*2){
          Toast.fail('上传文件大小最多不超过2M!');
          return;
        }
        let type = res.tempFiles[0].name.split(".")[1];
        if(type !=='png' &&  type !=='jpg'&&  type !=='pdf'&&  type !=='doc'&&  type !=='docx'){
          Toast.fail('上传附件格式错误!');
          return;
        }
        Toast.loading({
          duration: 0,
          message: '加载中...',
          forbidClick: true,
        });
        wx.uploadFile({
          url: ApiRootUrl + '/hire/uploadResume', 
          filePath: res.tempFiles[0].path,
          name: 'file',
          formData:{
            openId:wx.getStorageSync('openId'),
            oldPath:self.data.oldPath || ''
          },
          header: {
            "Content-Type": "multipart/form-data",
            'cookie': wx.getStorageSync("sessionid")
          },
          success(res) {
            Toast.clear();
            let data = JSON.parse(res.data);
            self.setData({
              oldPath:data.data,
              isuploadText:'已上传'
            })
            console.log('文件请求成功！！---',data)
            Toast.success('简历上传成功!');
          },
          fail(res) {
            Toast.clear();
            console.log(res)
          }
        })
      }
    })
  },
  applicationClick(){
    let self = this;
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    let params = {
      openId:wx.getStorageSync('openId')
    }
    wx.request({
      url: `${ApiRootUrl}/hire/checkJobStationResume`,
      method: 'get',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      params:params,
      success(res) {
        Toast.clear();
        if(res.data.code == 20000){
          console.log('res.data.data-----',res.data.data);
        let dataobj = res.data.data;
        self.setData({
          // iseditText:dataobj.attachmentFlag?'已上传': '未上传',
          isuploadText:dataobj.attachmentFlag?'已上传': '未上传',
          // isuploadText:dataobj.resumeFlag?'已编辑': '未编辑'
        })
         if(!dataobj.attachmentFlag){
            self.setData({
              appilicationShow:true
            })
         }else{
            self.applicationmethodClick();
         }                      
        }else{
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res){
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  applicationmethodClick(){
    let self = this;
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    let params = {
      jobId:wx.getStorageSync('zhaopinId'),
      orgCode:self.data.orgCode,
      stationId:self.data.gangweiId,
      openId:wx.getStorageSync('openId'),
      name:wx.getStorageSync('userinfo').name,
      identity:wx.getStorageSync('userinfo').identity,
    }
    wx.request({
      url: `${ApiRootUrl}/hire/applyJobStation`,
      method: 'post',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      params:params,
      success(res) {
        Toast.clear();
        if(res.data.code == 20000){
          console.log(res.data.data);
          Toast.success('申请成功！')
        }else{
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res){
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  queryDetails(id){
    let self = this;
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    let params = {
      id:id
    }
    wx.request({
      url: `${ApiRootUrl}/hire/getStationById`,
      method: 'get',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      params:params,
      success(res) {
        Toast.clear();
        if(res.data.code == 20000){
          console.log(res.data.data);
          let dataobj = res.data.data;
          self.setData({
            daiyuName:dataobj.stationName,
            isjipin:dataobj.jobStatus == 1?true:false,
            xinzi:`${dataobj.beginSalary}-${dataobj.endSalary}元/月`,
            tagList:dataobj.tagList,
            postionDesc:dataobj.stationDesc,
            managerPhone:dataobj.managerPhone,
            orgCode:dataobj.orgCode,
            gangweiId:dataobj.id
          })
          wx.setNavigationBarTitle({
            title: dataobj.stationName
          })
        }else{
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res){
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    console.log(id);
    this.queryDetails(id);
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})